public class NewJobAppWizard {

    // these properties will be used to maintain state
    
    public List<Candidate__c> results {
    	get{
    		if(results == null) results = new List<Candidate__c>();
        	return results;
    	}
    	set;	
    }
    
    public String searchText {
    	get;
    	set{
    		value = value.endsWith('*') ? value : value + '*';
            searchText = value; 
    	}
    }
    
    public Boolean showSearch {
    	get{
    		if (showSearch == null) showSearch = true;
    		return showSearch;
    	}
    	set;	
    }

    public Job_Application__c jobApplication { get; set; }
    
    public Candidate__c candidate {
    	get{
            if (candidate == null) candidate = new Candidate__c();
            return candidate;
    	}
    	set;
    }
    
    public String candidateId { get;set; }
    
    public NewJobAppWizard(ApexPages.StandardController stdController) {
    	// constructor
        this.jobApplication = (Job_Application__c)stdController.getRecord();
        if ((System.currentPageReference().getParameters().get('posId') != null)&&
            		(System.currentPageReference().getParameters().get('posId') != '')){
        	jobApplication.Position__c = System.currentPageReference().getParameters().get('posId');
        } 
    }
        
    // The next 3 methods control navigation through
    // the wizard. Each returns a PageReference for one of the 3 pages
    // in the wizard. Note that the redirect attribute does not need to
    // be set on the PageReference because the URL does not need to change
    // when users move from page to page.
    public PageReference step1() {
            return Page.jobAppStep1;
    }
    public PageReference step2() {
    	if (jobApplication.Position__c != null){
            return Page.jobAppStep2;
    	} else {
    		return null;
    	}
    }
    public PageReference step3() {
    	if (jobApplication.Position__c == null){
    		return Page.jobAppStep1;
    	} else if (candidate.last_name__c == null){
    		return Page.jobAppStep2;
    	} else{
            return Page.jobAppStep3;
    	}
    }
    
    public PageReference doSearch() {
        results = (List<Candidate__c>)[FIND :searchText IN ALL FIELDS RETURNING Candidate__c(Id, First_Name__c, Last_Name__c, Email__c, City__c, State_Province__c, Phone__c)] [0];
        // firstTime = false;
        return null;
    }
    
    public PageReference SelectCandidate(){       
        candidate = [select id,first_name__c,last_name__c,phone__c,email__c,mobile__c, street_address_1__c,street_address_2__c,city__c,state_province__c,zip_postal_code__c,country__c from candidate__c where id=:candidateId];   
        return Page.jobAppStep3;      
    }
    
    public PageReference NewCandidate(){
    	if (!showSearch) {
    		candidate = new Candidate__c();	
    		candidateId = null; // reset the id so we know what to do upon save
    	}	
    	return null;
    }
         
        public PageReference save() {
        	if (candidateId == null) {
        		// it's a brand spanking new candidate so we need to insert it first
        		try{
        			insert candidate;   
        			System.debug('new candidate=' + candidate);  
        		} catch (System.DmlException e) {
					for (Integer i = 0; i < e.getNumDml(); i++) {
						// Process exception here
						System.debug(e.getDmlMessage(i));
					} 
        		}		
        	}
        	System.debug('jobApplication=' + jobApplication);
        	jobApplication.Candidate__c = candidate.id;
        	try{
        		insert jobApplication;
        	} catch (System.DmlException e) {
				for (Integer i = 0; i < e.getNumDml(); i++) {
					// Process exception here
					System.debug(e.getDmlMessage(i));
				} 
    		}
        	
            String sUrl = '/' + jobApplication.id;
            PageReference jaPageRef = new PageReference(sUrl);
            return jaPageRef;   
        }
        
        public PageReference cancel() {       	
            String sUrl = '/a00/o';
            PageReference jaPageRef = new PageReference(sUrl);
            return jaPageRef;  
        }
}